Welcome back.
So you've already seen the for
loop as a way to iterate over every item in a sequence.
A while loop is a much more general way of iterating.
So a while loop is kind of like a hybrid between a for statement and
an if statement.
So a while loop looks like this.
You say while, and then just like an if statement,
you have a conditional that comes after the while.
And then you have a piece of code that will run if this condition is true.
But unlike in if statement where after you're done running this piece of code,
if you use a while instead.
Then by the time you get to this end of code, then we loop back up here and
check now is this condition is still true.
So in the sense that it loops back up to the top, that's kind of like a for
statement.
So again, when we get to a while statement, we check,
is this condition true?
If the condition is true, then we run this piece of code and
then we go back up and check is this condition still true.
So, if this condition is not true, just like in if statement,
then we skip this block of code and execute the next code block.
So, let's see this in action.
So, here I have some code that's going to take the sum of numbers one plus two,
plus three, plus whatever, and we pass in.
So here, we pass in a number called aBound as an argument to our sumTo function.
And we're using a while loop in order to do this.
So on line 11, we print out the value of sumTo(4),
which should be one plus two, plus three, plus four, which is going to give us ten.
And then here we print out the sumTo(1000),
which is going to be a much larger number.
So first I'm just going to run my code,
just to make sure that it still works correctly.
I'm good, so I see I get I get ten here, and I get this number from line 13.
Okay, so now that we've done that, let's look a little bit at how it works.
So, you see that we initialized two variables here.
So we start up this sum to be zero, and
this is going to be kind of a kin to our accumulator variable.
So, what it's going to keep track of is theSum.
So far.
And then we have this other variable, which we call aNumber.
And what this is going to keep track of is where we are.
And that's going to start out as one, but then we're going to set it to two,
to three, and then so on until we get to n, which in this case is aBound.
So now here we have a while statement.
And our condition is while aNumber is less than or equal to aBound,
again aBound here is whatever we're adding up to.
So as long as our aNumber variable is less than or equal to aBound.
Then what we do is we first say, theSum equals its previous value plus a number.
So when a number starts out as one, then theSum is going to be zero plus one.
Just going to be one.
So then after that, what we do is we say aNumber equals aNumber plus one.
Again, aNumber keeps track of where we are.
So aNumber is going to start out as one, and
then it's going to two, and then three, and then four,
and so on until aNumber is less than or equal to aBound.
And again, aBound here is the number that we're adding up to.
So it's going to keep going up by one until we get to aBound.
And as we're doing that, as we're incrementing this aNumber,
then we're adding that number to theSum that we have so far.
And then by the time this while loop is done running,
theSum is going to have the correct answer.
But let's inspect this code just to make it a little bit more clear.
So I'm going to open up code lines, and here I have the same piece of code,
except I'm only printing out sumTo when called with four.
So, the first thing that we're going to do is evaluate the function, so
we can see that sumTo is the function that we declared right here.
And then we print out the value of sumTo when called with four.
So, what the means is that aBound is going to have the value of four.
And then we start out the theSum with value zero, and aNumber with one.
Remember, theSum keeps track of the sum so far.
And aNumber keeps track of where we are.
And now we're at the important bit, the while loop.
Again, we say while aNumber is less than or equal to aBound.
The first time that we run this, aNumber has the value one.
One is less than or equal to aBound, which is four, and so yes, we do run this code.
So in this code, we say theSum equals this sum plus aNumber.
So theSum is going to go from zero to one.
Then we say aNumber is aNumber plus one, so it goes from one to two.
Now, the next time we run this, we check,
is aNumber still less than or equal to aBound.
So in this case, aNumber is now two.
And we ask, is two less than or equal to four?
Yes it is.
And so we run the code in here, and we save theSum equals theSum plus aNumber.
aNumber is now two, so theSum is going to go from one, two, three.
So theSum is now three, and then aNumber gets its previous
value plus one, so aNumber is going to from two to three.
And we ask again, is aNumber less than or equal to aBound?
Yes, three is less than or equal to four so we run this code.
So theSum goes from three to three plus aNumber which is three.
So theSum goes to six, and aNumber is going to from three to four.
And then we ask, is four less than or equal to four?
Yes, it is, so theSum is going to go from six to six plus four, or ten.
And aNumber is going to go from four to four plus one or five.
And now here's another key point.
So now, aNumber is five.
And when we ask is five less than or equal to four?
It is not, so this condition is false.
Meaning that we're done running our while loop.
And we can see that the sum here has the value that we actually want.
So now, when we're done running our while, we skip to line nine,
which returns theSum, and we get ten, which is the correct answer.
So let's answer a few questions.
First, true or false, you can rewrite any for loop as a while loop.
Well, this is true.
A while loop is a much more general form of iteration.
It's capable of expressing what you can express in a for loop and
more, as we'll see in a bit.
This question asks,
which type of loop can be used to perform the following iteration?
You choose a positive integer at random, and
then print out the numbers one up to and including the selected integer.
In this case, we could actually use a for
loop if we wanted to, because we could use the range function.
And anytime you can use a for loop, you could also use a while loop.
So I'm going to say here that the answer is A.
In this question, we're asked to write a while loop that's initialized to zero and
stops at 15 if the counter is an even number.
Append the counter to a list called eve_nums.
So, I'm going to say count equals zero, and I want it to stop at 15,
so I'll say while count is less than or equal to 15.
And I'm just going to say count equals count plus one.
So, what we're doing in this code is we're initializing count to be zero.
And then inside of the while loop, as long as count is less than 15,
we assign it to value to its previous value plus one.
So count is going to go from zero to one, to two, to three, four, five,
six, seven, eight, nine, ten, 11, 12, 13, 14, and then 15.
And then it does get assigned to 16.
But as soon as it's 16, then we exit this while loop,
because 16 is not less than or equal to 15.
And so by the time we're done with this while loop, count is going to be 16.
Now this question is asking us to do a little bit more, it's saying if
the counter is an even number, append the counter to list called eve_nums.
So I can check if the counter is an even number
by saying if count modulo two is zero.
Again that's just saying if the remainder when divided by two is zero.
And what we want to do then is we want to append it to this list
which I'll call eve_nums.
I mean, to start out as an empty list and
if count is even, I want to say eve_nums.append count.
So let's test our code to be sure that it works.
Okay, so we can see that now our code passes all the tasks.
And even_nums has the values zero, two,
four, six, eight, and so on to 14.
This question says, below we've provided a for
loop that sums all of the elements of list1.
Write code that accomplishes the same tasks but instead uses a while loop.
Assign the accumulator variable to the name accum.
So the strategy that we're going to take in order to do this
is we're going to have one variable that's going to keep track of the current index.
So I'm going to call that variable idx.
idx is going to be zero, and then one, and then two, three, four, five, six.
And then by the time it's seven, we want it to stop because there isn't a 7th item.
So let's write the code to properly set idx first.
I'm going to say IDX equals zero,
and I'll say while idx is less
than the length of list1,
then idx equals idx plus one.
Now an important point here is that I said while idx is less than
the length of list1, and I initialized it to zero.
So why did I do both of these things?
Well, first I initialized it to zero because again list and
sequences are zero indexed.
Meaning that we have to start out at zero to get the first item.
Now, I said less then the length of this one, rather than less than or equal to,
because here this one is one, two, three, four, five, six, seven items long.
So the length of this one is seven.
But we only want to go until the last item.
And because we're zero index, we have the last item is actually item six.
So by the time we get to idx equals seven, then we want to breakout of this loop.
We only want idx to be zero, one, two, three, four, five, six.
So I'm going to say less than the length of this list, not less than or equal to.
Okay, now we don't just want to loop through all of the indices of this list.
We also want to assign a new variable accum to be the sum of every item.
So I'm going to start out accum equals zero,
to say that we haven't seen anything so far.
And then just like on line six here where we say total equals total
plus the value of the element.
I want to say accum equals accum, and
then I'm want to say, plus the value at index idx.
And I get that by saying list1 sub idx.
So when idx is zero, this is going to be eight.
When idx is one, this is going to be three and so on.
So again, as idx iterates through all of the indices.
This is going to be the value at that index, and
so we add the value at that index to the previously accumulated value.
Now let's run our code and be sure that it's correct.
So we can see that our code works as we wanted it to.
That's all for now, until next time.